package com.codi.api.controller.v2;

import com.codi.api.security.UserUtil;
import com.codi.base.exception.BaseAppException;
import com.codi.base.log.annotation.Loggable;
import com.codi.base.util.EqualsUtil;
import com.codi.bus.constant.RuleConstant;
import com.codi.bus.core.domain.Lookup;
import com.codi.bus.core.resultModel.BaseResult;
import com.codi.bus.core.resultModel.WalletDetailResult;
import com.codi.bus.core.resultModel.WalletDetailResultV2;
import com.codi.bus.core.resultModel.WalletListResult;
import com.codi.bus.core.service.LookupService;
import com.codi.bus.core.service.QueryService;
import com.codi.bus.core.service.T0Service;
import com.codi.bus.core.service.WalletService;
import com.codi.bus.core.service.model.FundModel;
import com.codi.bus.core.service.model.T0Model;
import com.codi.bus.core.service.model.TradeLimitModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
* describe
* @author sgx[sungaoxiang@cd121.com]
* @create 2017/10/20 17:57
**/
@Controller
@RequestMapping("/v2/wallet/")
public class WalletControllerV2 {

    private static final Logger logger = LoggerFactory.getLogger(WalletControllerV2.class);

    @Resource(name = "walletService")
    private WalletService walletService;
    @Resource(name="queryService")
    private QueryService queryService;
    @Resource
    private LookupService lookupService;
    @Resource
    private T0Service t0Service;
    /*
    获取所有的T0基金列表
     */
    @Loggable
    @RequestMapping(value = "all", method = RequestMethod.GET)
    @ResponseBody
    public WalletDetailResultV2 all() throws BaseAppException {
        Long userId = UserUtil.getLoginUserId();
        WalletDetailResultV2 result = walletService.getWalletDetailV2(userId);
        return result;
    }

    /**
     * 获取单个t0基金的详情
     * @param fundCode
     * @return
     * @throws BaseAppException
     */
    @Loggable
    @RequestMapping(value = "detail", method = RequestMethod.GET)
    @ResponseBody
    public WalletDetailResult detail(@RequestParam("fundCode") String fundCode) throws BaseAppException {
        Long userId = UserUtil.getLoginUserId();
        try{
            WalletDetailResult result = walletService.getWalletDetailByFundCode(userId,fundCode);
            result.setRecommendedFundModel(queryService.queryRecommendedFund());//设置推荐基金
            return result;
        }
        catch(Exception e){e.printStackTrace();}

        return null;
    }


    /**
     * 首页点击瑞福宝-跳转，瑞福宝列表
     *
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    public BaseResult getWalletList() {
        Long userId = UserUtil.getLoginUserId();
        WalletListResult walletListResult = new WalletListResult();
        try {
            WalletListResult.WalletData walletData = new WalletListResult.WalletData();
            WalletDetailResultV2 result = walletService.getWalletDetailV2(userId);
            walletData.setTi(result.getTi());
            walletData.setTotalIncome(result.getTotalIncome());
            walletData.setTotalWorthValue(result.getTotalWorthValue());
            walletData.setTwv(result.getTwv());
            walletData.setTotalTodayIncome(result.getTotalTodayIncome());
            walletData.setTti(result.getTti());
            walletData.setInTransitAmount(result.getInTransitAmount());
            List<T0Model> data = getT0Models();
            walletListResult.setT0Models(data);
            walletListResult.setWalletData(walletData);
        } catch (BaseAppException e) {
            e.printStackTrace();
        }
        return walletListResult;
    }


    /**
     * 赎回转购瑞富宝列表,日结转
     *
     * @return
     */
    @GetMapping("/sellForWalletChoose")
    @ResponseBody
    public BaseResult sellForWalletChoose() {
        BaseResult result = new BaseResult();
        List<T0Model> data = getT0Models();
        result.setResult(data);
        return result;
    }

    private List<T0Model> getT0Models() {
        Lookup lookup = lookupService.getLookup(RuleConstant.T0FUND_LIST_DISPLAY_FUNDS);
        List<T0Model> data;
        String fundReport = "R";
        if (EqualsUtil.equals(1, lookup.getRuleStatus().intValue())) {
            String valueStr = lookup.getRuleValue();
            String[] fundCodeArray = valueStr.split(",");
            data = t0Service.queryT0Funds(false, Arrays.asList(fundCodeArray));
        } else {
            data = t0Service.queryT0Funds(false);
        }
        data = data.stream().filter(t0Model -> fundReport.equals(t0Model.getFundReport())).collect(Collectors.toList());

        data.forEach(t0Model -> {
            FundModel fundModel = queryService.queryFund(t0Model.getFundCode());
            boolean canBuy = fundModel.getCanBuy();
            t0Model.setCanBuy(canBuy);
            List<TradeLimitModel> tradeLimitModels = queryService.queryTradeLimits(t0Model.getFundCode(), true);
            t0Model.setLimits(tradeLimitModels);
        });
        return data;
    }

}
